package com.example.springbootstartermqttprovider.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class CleanMessage {


    public static void main(String[] args) {


        ObjectMapper mapper = new ObjectMapper();

        String str = "{\"TYPE\":\"2\",\"ID\":\"1500000003\",\"PACKET\":[\"4\",\"4\"],\"DATA\":[\"ROADBIT0000024F1505D4A5C\",\"mobike000000C8F47EC46330\",\"QJDFV258000024F1505568A9\",\"mobike000000C81EB6C8D5D7\",\"XenoLink000024F15058C7B0\",\"XL-2008CACA0363857725096\",\"mobike000000C2443A1918B7\",\"ROADBIT0000024F150576E56\",\"ROADBIT0000024F1505CF2BF\",\"ROADBIT0000024F15058D3E7\",\"HB7010452020C4A828400B9C\",\"HB7010455342C4A8284067D2\",\"mobike000000EAAFE6EFBB74\",\"XiaoanTech00155750603076\",\"mobike000000F51BF8BB077C\",\"QJDFV258000024F15055500D\",\"mobike000000D936F0EFA04F\",\"mobike000000D9F81DA0CC27\",\"ROADBIT0000024F1505788BE\",\"mobike000000E2CE9CADE272\",\"VibeBit0000024F15058EAFF\",\"QCCT00000000C4A82840C2AC\",\"mobike000000E5E8B33B4D02\",\"mobike000000DE4B620A0B59\",\"mobike000000F3CD93A6A021\",\"QCCT00000000C4A82840D3A2\",\":</682000000D74F51B3FAF3\",\"mobike000000ED837F2F48C2\",\"ROADBIT0000024F15058C089\",\"QCCT00000000C4A82840D60A\",\"mobike000000D8C470501CC0\",\"mobike000000D15F44DA50CC\",\"HB7010439636C4A8283FEC3F\",\"mobike000000F7F614F12138\",\"mobike000000CCFAA3A5D1CB\",\"ROADBIT0000024F1505D06CE\",\"HB7010453457C4A828405C09\",\"ROADBIT0000024F15058BFF0\",\"mobike000000E2EAA8B5D147\",\"mobike000000DFCB80A75084\",\"ROADBIT0000024F15058F668\",\"mobike000000F1E4236F0483\",\"ROADBIT0000024F1505D364C\",\"mobike000000FE725E120D46\",\"mobike000000D4A636EEC8D4\",\"mobike000000EAF43AB8E412\",\"QCCT00000000C4A82840C033\",\"mobike000000CFA692025D94\",\"mobike000000E7740F9C8B79\",\"mobike000000C00EC6DC9F61\",\"HWS633500000C7E000056335\"]}";
        String prefix = StrUtil.subBefore(str, "\"DATA\":", false) + "\"DATA\":";
        // 提取 DATA 部分并去掉外层的方括号
        String dataSection = StrUtil.subAfter(str, "\"DATA\":", false);
        String cleanedData = dataSection.substring(1, dataSection.length() - 2); // 去掉外层的引号和最后一个逗号
        // 分割字符串为数组
        String[] items = cleanedData.split("\",\"");
        List<String> list = new ArrayList<>();
        // 遍历数组，确保每个元素的长度为24，进行处理
        for (String item : items) {
            item = item.replaceAll("^\"|\"$", "").trim(); // 去掉开头和结尾的引号
            // 若长度不够则补零，超过则截取
            if (item.length() < 24) {
                item = String.format("%-24s", item).replace(' ', '0'); // 左补零
            } else if (item.length() > 24) {
                item = item.substring(0, 24); // 截断
            }
            list.add(item);
        }
        System.out.println(prefix + JSONUtil.toJsonStr(list) + "}");
        String cleanedMessage = prefix + JSONUtil.toJsonStr(list) + "}";
        try {
            JsonNode rootNode = mapper.readTree(cleanedMessage);
            System.out.println(rootNode);

        }catch (Exception e){
            e.printStackTrace();
        }

        // 生成最终的 JSON 字符串






    }
}
